package scau.erps.view.base;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;

import scau.erps.app.Erps;
import scau.erps.entity.Loan;
import scau.erps.entity.Material;
import scau.erps.entity.Research;
import scau.erps.entity.Sand;
import scau.erps.entity.Team;
import scau.erps.service.FactoryService;
import scau.erps.service.LoanService;
import scau.erps.service.MaterialService;
import scau.erps.service.ResearchService;
import scau.erps.service.SandService;
import scau.erps.service.TeamOrderService;
import scau.erps.service.impl.FactoryServiceImpl;
import scau.erps.service.impl.LoanServiceImpl;
import scau.erps.service.impl.MaterialServiceImpl;
import scau.erps.service.impl.ResearchServiceImpl;
import scau.erps.service.impl.SandServiceImpl;
import scau.erps.service.impl.TeamOrderServiceImpl;
import scau.erps.util.CalculateQuarter;
import scau.erps.view.SandPanel;
import scau.erps.view.system.team.MyOrderBiddingDialog;

public class DecisionPanel extends ButtonPanel {
	private static final long serialVersionUID = 1L;
	private SandPanel dashBoardPanel;
	private JPanel beginOfYearPanel, middleOfYearPanel, endOfYearPanel;
	private SchedulePanel schedule;
	private Team team = Erps.getCurrentTeam();
	private SandService sandService = Erps.getIocInstance().get(SandServiceImpl.class);
	private LoanService loanService = Erps.getIocInstance().get(LoanServiceImpl.class);
	private MaterialService materialService = Erps.getIocInstance().get(MaterialServiceImpl.class);
	private FactoryService factoryService = Erps.getIocInstance().get(FactoryServiceImpl.class);
	public DecisionPanel(SandPanel dashBoardPanel) {
		this.dashBoardPanel = dashBoardPanel;
		JPanel decisionPanel = new JPanel();
		decisionPanel.setLayout(new BorderLayout());
		JPanel panel = new JPanel(new BorderLayout());
		schedule = new SchedulePanel();
		panel.add(schedule, BorderLayout.NORTH);
		panel.add(getBeginOfYearPanel(), BorderLayout.CENTER);
		decisionPanel.add(panel, BorderLayout.NORTH);
		decisionPanel.add(getMiddleOfYearPanel(), BorderLayout.CENTER);
		decisionPanel.add(getEndOfYearPanel(), BorderLayout.SOUTH);
		this.add(decisionPanel);
	}

	/**
	 * 年初决策面板
	 * 
	 * @return
	 */
	private JPanel getBeginOfYearPanel() {
		if (beginOfYearPanel == null) {
			beginOfYearPanel = new JPanel();
		}
		beginOfYearPanel.setBorder(BorderFactory.createTitledBorder("年初决策"));
		beginOfYearPanel.add(btn[0]);
		beginOfYearPanel.add(btn[1]);
		beginOfYearPanel.add(btn[2]);
		beginOfYearPanel.add(btn[3]);
		return beginOfYearPanel;
	}

	/**
	 * 年中决策面板
	 * 
	 * @return
	 * @author 郑剑川
	 */
	private JPanel getMiddleOfYearPanel() {
		if (middleOfYearPanel == null) {
			middleOfYearPanel = new JPanel();
		}
		JPanel[] panel = new JPanel[11];
		for (int i = 0; i < panel.length; i++) {
			panel[i] = new JPanel(new FlowLayout(FlowLayout.LEFT));
		}
		panel[0].add(new JLabel("(1)"));
		panel[0].add(btn[4]);
		panel[0].add(btn[5]);
		panel[0].add(btn[6]);
		panel[1].add(new JLabel("(2)"));
		panel[1].add(btn[7]);
		panel[1].add(btn[8]);
		panel[2].add(new JLabel("(3)"));
		panel[2].add(btn[9]);
		panel[2].add(btn[10]);
		panel[3].add(new JLabel("(4)"));
		panel[3].add(btn[11]);
		panel[4].add(new JLabel("(5)"));
		panel[4].add(btn[12]);
		panel[4].add(btn[13]);
		panel[5].add(new JLabel("(6)"));
		panel[5].add(btn[14]);
		panel[5].add(btn[15]);
		panel[5].add(btn[16]);
		panel[6].add(new JLabel("(7)"));
		panel[6].add(btn[17]);
		panel[7].add(new JLabel("(8)"));
		panel[7].add(btn[18]);
		panel[8].add(new JLabel("(9)"));
		panel[8].add(btn[19]);
		panel[8].add(btn[20]);
		panel[9].add(new JLabel("(10)"));
		panel[9].add(btn[21]);
		panel[10].add(new JLabel("(11)"));
		panel[10].add(btn[22]);
		middleOfYearPanel.setBorder(BorderFactory.createTitledBorder("年中决策"));
		middleOfYearPanel.setLayout(new GridLayout(11, 1));
		for (int i = 0; i < panel.length; i++) {
			middleOfYearPanel.add(panel[i]);
		}
		return middleOfYearPanel;
	}

	/**
	 * 年末决策面板
	 * 
	 * @return
	 */
	private JPanel getEndOfYearPanel() {
		if (endOfYearPanel == null) {
			endOfYearPanel = new JPanel();
		}
		endOfYearPanel.setBorder(BorderFactory.createTitledBorder("年末决策"));
		endOfYearPanel.setLayout(new GridLayout(3, 1));
		JPanel[] panel = new JPanel[3];
		for (int i = 0; i < panel.length; i++) {
			panel[i] = new JPanel(new FlowLayout(FlowLayout.LEFT));
		}
		panel[0].add(new JLabel("(1)"));
		panel[0].add(btn[23]);
		panel[0].add(btn[24]);
		panel[0].add(btn[25]);
		panel[1].add(new JLabel("(2)"));
		panel[1].add(btn[26]);
		panel[1].add(btn[27]);
		panel[1].add(btn[28]);
		panel[2].add(new JLabel("(3)"));
		panel[2].add(btn[29]);
		panel[2].add(btn[30]);
		panel[2].add(btn[31]);
		for (int i = 0; i < panel.length; i++) {
			endOfYearPanel.add(panel[i]);
		}
		return endOfYearPanel;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		/*** 年度规划 ****/
		if (e.getSource() == btn[0]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 1) {
				sandService.clearCost(team.getId());
				JOptionPane.showMessageDialog(null, "开始讨论本年度策略", "提示", JOptionPane.INFORMATION_MESSAGE);
				this.changeStep();
				this.dashBoardPanel.setDashBoardData();
			}

		}
		/*** 支付应付税 ****/
		else if (e.getSource() == btn[1]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 2) {
				// TODO 计算应付税
				this.changeStep();
				this.dashBoardPanel.setDashBoardData();
			}
		}

		/*** 订单竞价 ****/
		else if (e.getSource() == btn[2]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 3) {
				new MyOrderBiddingDialog();
				this.changeStep();	
			}
		}

		/*** 登记订单 ****/
		else if (e.getSource() == btn[3]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 4) {
				TeamOrderService orderService = Erps.getIocInstance().get(TeamOrderServiceImpl.class);
				int n = orderService.recordOrders();
				JOptionPane.showMessageDialog(null, n+"张订单登记成功，请在我的业务中查看订单详细信息", "提示", JOptionPane.INFORMATION_MESSAGE);
				this.changeStep();
				this.dashBoardPanel.setDashBoardData();
			}
		}

		/*** 更新短贷 ****/
		else if (e.getSource() == btn[4]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 5) {
				JOptionPane.showMessageDialog(null, "系统自动更新，请核对操作盘面", "提示", JOptionPane.INFORMATION_MESSAGE);
				this.changeStep();
				this.dashBoardPanel.setDashBoardData();

			}
		}

		/*** 偿还短贷 ****/
		else if (e.getSource() == btn[5]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 6) {
				List<Loan> shortLoans = loanService.getReturnLoans(team.getId(), "短贷");
				List<Loan> highLoans = loanService.getReturnLoans(team.getId(), "高利贷");
				int n = shortLoans.size()+highLoans.size();
				int amount = 0;
				int interest = 0;
				for (Loan loan : shortLoans) {
					amount = amount + loan.getAmount();
					interest = interest + loan.getInterest();
				}
				for (Loan loan : highLoans) {
					amount = amount + loan.getAmount();
					interest = interest + loan.getInterest();
				}
				int cash = Erps.getSand().getCash();				
				if(cash>=(amount+interest)){
					loanService.repayShortLoan(shortLoans);
					loanService.repayShortLoan(highLoans);
					String message = n+"笔短贷款合计"+amount+"M已成功还款,利息为"+interest+ "M,请更新盘面";
					JOptionPane.showMessageDialog(null, message, "提醒", JOptionPane.INFORMATION_MESSAGE);
					this.changeStep();
					this.dashBoardPanel.setDashBoardData();
				}else{
					JOptionPane.showMessageDialog(null, "当前现金不足偿还短贷、高利贷以及利息，请先融资！", "警告", JOptionPane.WARNING_MESSAGE);
				}				
			}
		}

		/*** 申请短贷 ****/
		else if (e.getSource() == btn[6]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 7) {
				String[] amount = { "20", "40", "60" };
				// TODO 加入规则，小于所有者权益2倍
				String s = (String) JOptionPane.showInputDialog(null, "请选择贷款额度:\n", "申请短贷", JOptionPane.PLAIN_MESSAGE, null, amount, "20");
				if (s != null) {									
					loanService.applyShortLoan(Integer.parseInt(s));
					String info = "申请贷款" + s + "M成功，请前往银行领取现金！";	
					JOptionPane.showMessageDialog(null, info, "提醒", JOptionPane.INFORMATION_MESSAGE);
				}
				this.changeStep(); 
				this.dashBoardPanel.setDashBoardData();
			}
		}

		/*** 更新应付款 ****/
		else if (e.getSource() == btn[7]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 8) {
				JOptionPane.showMessageDialog(null, "系统自动更新，请核对操作盘面", "提示", JOptionPane.INFORMATION_MESSAGE);
				this.changeStep(); 
				this.dashBoardPanel.setDashBoardData();
			}
		}
		
		/*** 归还应付款 ****/
		else if (e.getSource() == btn[8]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 9) {
				List<Material> materialOrderList = materialService.getPayMaterialOrder(team.getId(), Erps.getSand().getTime());
				int tot = materialOrderList.size();
				int pay = 0;
				for (Material material : materialOrderList) {
					pay = pay + material.getNumber();
				}
				if (materialService.payMaterialOrder(team.getId(), Erps.getSand().getTime())) { // 当前现金可以支付本季度应付款
					String message = "本季度: " + Erps.getSand().getTime() + " \n" + "已支付: " + tot + " 笔订单" + " \n" + "支出现金: " + pay + " M" + " \n" + "请更新盘面";
					JOptionPane.showMessageDialog(null, message, "提示", JOptionPane.INFORMATION_MESSAGE);
					this.changeStep();
					this.dashBoardPanel.setDashBoardData();
				} else {
					String message = "本季度: " + Erps.getSand().getTime() + " \n" + "应支付: " + tot + " 笔订单" + " \n" + "需要现金: " + pay + " M" + " \n" + "当前现金不足，请先进行融资操作";
					JOptionPane.showMessageDialog(null, message, "提示", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		}
		
		/*** 更新原材料订单 ****/
		else if (e.getSource() == btn[9]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 10) {
				JOptionPane.showMessageDialog(null, "系统自动更新，请核对操作盘面", "提示", JOptionPane.INFORMATION_MESSAGE);
				this.changeStep(); 
				this.dashBoardPanel.setDashBoardData();
			}
		}
		
		/*** 原材料订单入库  ****/	
		else if (e.getSource() == btn[10]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 11) {
				materialService.updateStore(team.getId(), Erps.getSand().getTime());
				List<Material> materialOrderList = materialService.getGetMaterialOrder(team.getId(), Erps.getSand().getTime());
				int tot, m1 = 0, m2 = 0, m3 = 0, m4 = 0;
				tot = materialOrderList.size();
				for (Material material : materialOrderList) {
					if (material.getType().equals("M1"))
						m1 = m1 + material.getNumber();
					if (material.getType().equals("M2"))
						m2 = m2 + material.getNumber();
					if (material.getType().equals("M3"))
						m3 = m3 + material.getNumber();
					if (material.getType().equals("M4"))
						m4 = m4 + material.getNumber();
				}
				String message = "本季度: " + Erps.getSand().getTime() + " \n" + "入库订单： " + tot + " 笔" + " \n" + "入库原料M1： " + m1 + " 个" + " \n" + "入库原料M2： " + m2 + " 个" + " \n" + "入库原料M3： " + m3 + " 个"
						+ " \n" + "入库原料M4： " + m4 + " 个" + " \n";
				JOptionPane.showMessageDialog(null, message, "提示", JOptionPane.INFORMATION_MESSAGE);
				this.changeStep();
				this.dashBoardPanel.setDashBoardData();
			}
		}
		
		/*** 下原材料订单  ****/
		else if (e.getSource() == btn[11]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 12) {
				MaterialOrderDialog dialog = new MaterialOrderDialog();
				dialog.setVisible(true);
				this.changeStep();
				this.dashBoardPanel.setDashBoardData();
			}
		}
		/*** 更新生产  ****/
		else if (e.getSource() == btn[12]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 13) {				
				this.changeStep();
				this.dashBoardPanel.setDashBoardData();
			}
		}
		/*** 完工入库  ****/
		else if (e.getSource() == btn[13]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 14) {				
				this.changeStep();
				this.dashBoardPanel.setDashBoardData();
			}
		}
		/*** 投资生产线  ****/
		else if (e.getSource() == btn[14]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 15) {				
				this.changeStep();
				this.dashBoardPanel.setDashBoardData();
			}
		}
		/*** 生产线转产  ****/
		else if (e.getSource() == btn[15]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 16) {				
				this.changeStep();
				this.dashBoardPanel.setDashBoardData();
			}
		}
		/*** 变卖生产线  ****/
		else if (e.getSource() == btn[16]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 17) {				
				this.changeStep();
				this.dashBoardPanel.setDashBoardData();
			}
		}
		/*** 开始下一批生产  ****/
		else if (e.getSource() == btn[17]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 18) {				
				this.changeStep();
				this.dashBoardPanel.setDashBoardData();
			}
		}
		
		/*** 产品研发  ****/
		else if (e.getSource() == btn[18]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 19) {	
				JCheckBox crystalCheckBox = new JCheckBox("Crystal");
				JCheckBox rubyCheckBox = new JCheckBox("Ruby");
				JCheckBox sapphireCheckBox = new JCheckBox("Sapphire");
				int team_Id = Erps.getCurrentTeam().getId();
				ResearchService researchService = Erps.getIocInstance().get(ResearchServiceImpl.class);
				if (researchService.findResearchs(team_Id, "Crystal") == 4) {
					crystalCheckBox.setEnabled(false);
				}
				if (researchService.findResearchs(team_Id, "Ruby") == 4) {
					rubyCheckBox.setEnabled(false);
				}
				if (researchService.findResearchs(team_Id, "Sapphire") == 6) {
					sapphireCheckBox.setEnabled(false);
				}
				JPanel panel = new JPanel();
				panel.add(crystalCheckBox);
				panel.add(rubyCheckBox);
				panel.add(sapphireCheckBox);
				JPanel researchPanel = new JPanel(new BorderLayout());
				researchPanel.add(new JLabel("请选择需要研发的产品?"), BorderLayout.CENTER);
				researchPanel.add(panel, BorderLayout.SOUTH);
				int n = JOptionPane.showConfirmDialog(null, researchPanel, "产品研发投资", JOptionPane.OK_CANCEL_OPTION);
				if (n == JOptionPane.YES_OPTION) {
					int total = 0;
					if (crystalCheckBox.isSelected()) {
						Research research = new Research();
						research.setTeam_Id(Erps.getCurrentTeam().getId());
						research.setType("Crystal");
						research.setTime(Erps.getSand().getTime());
						research.setAmount(1);
						research.setName("产品研发");
						researchService.researchInvest(research);
						total = total + 1;
					}
					if (rubyCheckBox.isSelected()) {
						Research research = new Research();
						research.setTeam_Id(Erps.getCurrentTeam().getId());
						research.setType("Ruby");
						research.setTime(Erps.getSand().getTime());
						research.setAmount(2);
						research.setName("产品研发");
						researchService.researchInvest(research);
						total = total + 2;
					}
					if (sapphireCheckBox.isSelected()) {
						Research research = new Research();
						research.setTeam_Id(Erps.getCurrentTeam().getId());
						research.setType("Sapphire");
						research.setTime(Erps.getSand().getTime());
						research.setAmount(2);
						research.setName("产品研发");
						researchService.researchInvest(research);
						total = total + 2;
					}
					JOptionPane.showMessageDialog(null, "共需支付产品研发费用" + total + "M");
					this.changeStep();
					this.dashBoardPanel.setDashBoardData();
				}else{
					this.changeStep();
				}
			}

		}
		/*** 更新应收款  ****/
		else if (e.getSource() == btn[19]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 20) {				
				this.changeStep();
				this.dashBoardPanel.setDashBoardData();
			}
		}
		/*** 应收款收现  ****/
		else if (e.getSource() == btn[20]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 21) {				
				this.changeStep();
				this.dashBoardPanel.setDashBoardData();
			}
		}
		/*** 按订单交货  ****/
		else if (e.getSource() == btn[21]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 22) {				
				this.changeStep();
				this.dashBoardPanel.setDashBoardData();
			}
		}
		/*** 支付行政管理费  ****/
		else if (e.getSource() == btn[22]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 23) {	
				int currentQuarter = CalculateQuarter.getCurrentQuarter(Erps.getSand().getTime());	
				Sand sand = Erps.getSand();
				int cash = sand.getCash();
				int managementFee = sand.getManagementFee();
				String time = sand.getTime();
				if(currentQuarter == 4){
					sand.setCash(cash-1);
					sand.setManagementFee(managementFee+1);
					sandService.updateSand(sand);
					this.changeStep();
				} else{	
					sand.setCash(cash-1);
					sand.setManagementFee(managementFee+1);
					sand.setTime(CalculateQuarter.addOneQuarter(time));
					sand.setStep("5");			
					sandService.updateSand(sand);
					this.btn[22].setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
					this.btn[4].setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.green));					
				}	
				this.schedule.setSeheduleData();
				this.dashBoardPanel.setDashBoardData();
			}
		}

		/*** 更新长贷  ****/
		else if (e.getSource() == btn[23]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 24) {
				JOptionPane.showMessageDialog(null, "系统自动更新，请核对操作盘面", "提示", JOptionPane.INFORMATION_MESSAGE);
				this.changeStep(); 
				this.dashBoardPanel.setDashBoardData();

			}
		}
		/*** 偿还长贷  ****/
		else if (e.getSource() == btn[24]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 25) {
				int i = loanService.getLoans(Erps.getCurrentTeam().getId(), "长贷", "已审核").size();
				int j = loanService.getReturnLoans(Erps.getCurrentTeam().getId(), "长贷").size();
				if(i>0){
					if(loanService.repayLongLoan(team.getId())){
						String message = i+"笔长贷支付利息成功!"+j+"笔长贷还款成功！";
						JOptionPane.showMessageDialog(null, message, "提示", JOptionPane.INFORMATION_MESSAGE);
						this.changeStep(); 
						this.dashBoardPanel.setDashBoardData();						
					}else{
						JOptionPane.showMessageDialog(null, "当前现金不足以偿还长贷和付息，请先融资！", "警告", JOptionPane.WARNING_MESSAGE);
					}
					
				}else{
					JOptionPane.showMessageDialog(null, "当前没有长贷", "提示", JOptionPane.INFORMATION_MESSAGE);
					this.changeStep(); 
				}				
			}
		}

		/*** 申请长贷  ****/
		else if (e.getSource() == btn[25]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 26) {
				String[] amount = { "10", "20", "30", "40", "50", "60", "70", "80", "90", "100", "110", "120" };
				String[] year = { "1Y", "2Y", "3Y", "4Y"};				
				JPanel panel = new JPanel();
				JComboBox<String> amountCombo = new JComboBox<String>(amount);
				JComboBox<String> yearCombo = new JComboBox<String>(year);
				panel.add(amountCombo);
				panel.add(yearCombo);				
				JPanel loanPanel = new JPanel(new BorderLayout());
				loanPanel.add(new JLabel("请选择需要长贷金额和年限?"), BorderLayout.CENTER);
				loanPanel.add(panel, BorderLayout.SOUTH);
				int n = JOptionPane.showConfirmDialog(null, loanPanel, "产品研发投资", JOptionPane.OK_CANCEL_OPTION);
				if (n == JOptionPane.YES_OPTION) {
					String s = (String) amountCombo.getSelectedItem();
					int y = yearCombo.getSelectedIndex()+1;
					System.out.println(s);
					System.out.println(y);
					loanService.applyLongLoan(Integer.parseInt(s),y);
					String info = "申请贷款" + s + "M成功，请前往银行领取现金！";					
					JOptionPane.showMessageDialog(null, info, "提醒", JOptionPane.INFORMATION_MESSAGE);					
				}				
				this.changeStep(); 
				this.dashBoardPanel.setDashBoardData();
			}
		}
		
		/*** 支付设备维修费  ****/
		else if (e.getSource() == btn[26]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 27) {	
				this.changeStep();			
				this.dashBoardPanel.setDashBoardData();
			}
		}
		
		/***支付租金  ****/
		else if (e.getSource() == btn[27]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 28) {
				int pay = factoryService.payRentFactoy(team.getId());
				if (pay >= 0) {
					String message = "本年度支付租金：" + pay + "M";
					JOptionPane.showMessageDialog(null, message, "提示", JOptionPane.INFORMATION_MESSAGE);
					this.changeStep(); // 切换步骤
					this.dashBoardPanel.setDashBoardData();
				} else
					JOptionPane.showMessageDialog(null, "当前现金不足，请先进行融资操作", "提示", JOptionPane.INFORMATION_MESSAGE);
			}
		}

		/*** 折旧 ****/
		else if (e.getSource() == btn[28]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 29) {				
				this.changeStep();
				this.dashBoardPanel.setDashBoardData();
			}
		}
		
		/***市场开拓  ****/ 
		else if (e.getSource() == btn[29]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 30) {
				JCheckBox districtCheckBox = new JCheckBox("区域");
				JCheckBox domesticCheckBox = new JCheckBox("国内");
				JCheckBox asiaCheckBox = new JCheckBox("亚洲");
				JCheckBox internationCheckBox = new JCheckBox("国际");
				int team_Id = Erps.getCurrentTeam().getId();
				ResearchService researchService = Erps.getIocInstance().get(ResearchServiceImpl.class);
				if (researchService.findResearchs(team_Id, "区域") == 1) {
					districtCheckBox.setEnabled(false);
				}
				if (researchService.findResearchs(team_Id, "国内") == 2) {
					domesticCheckBox.setEnabled(false);
				}
				if (researchService.findResearchs(team_Id, "亚洲") == 3) {
					asiaCheckBox.setEnabled(false);
				}
				if (researchService.findResearchs(team_Id, "国际") == 4) {
					internationCheckBox.setEnabled(false);
				}
				JPanel panel = new JPanel();
				panel.add(districtCheckBox);
				panel.add(domesticCheckBox);
				panel.add(asiaCheckBox);
				panel.add(internationCheckBox);
				JPanel researchPanel = new JPanel(new BorderLayout());
				researchPanel.add(new JLabel("请选择需要开拓的市场?"), BorderLayout.CENTER);
				researchPanel.add(panel, BorderLayout.SOUTH);
				int n = JOptionPane.showConfirmDialog(null, researchPanel, "市场开拓投资", JOptionPane.OK_CANCEL_OPTION);
				if (n == JOptionPane.YES_OPTION) {
					int total = 0;
					if (districtCheckBox.isSelected()) {
						Research research = new Research();
						research.setTeam_Id(Erps.getCurrentTeam().getId());
						research.setType("区域");
						research.setTime(Erps.getSand().getTime());
						research.setAmount(1);
						research.setName("市场开拓");
						researchService.researchInvest(research);
						total = total + 1;
					}
					if (domesticCheckBox.isSelected()) {
						Research research = new Research();
						research.setTeam_Id(Erps.getCurrentTeam().getId());
						research.setType("国内");
						research.setTime(Erps.getSand().getTime());
						research.setAmount(1);
						research.setName("市场");
						researchService.researchInvest(research);
						total = total + 1;
					}
					if (asiaCheckBox.isSelected()) {
						Research research = new Research();
						research.setTeam_Id(Erps.getCurrentTeam().getId());
						research.setType("亚洲");
						research.setTime(Erps.getSand().getTime());
						research.setAmount(1);
						research.setName("市场开拓");
						researchService.researchInvest(research);
						total = total + 1;
					}
					if (internationCheckBox.isSelected()) {
						Research research = new Research();
						research.setTeam_Id(Erps.getCurrentTeam().getId());
						research.setType("国际");
						research.setTime(Erps.getSand().getTime());
						research.setAmount(1);
						research.setName("市场开拓");
						researchService.researchInvest(research);
						total = total + 1;
					}
					JOptionPane.showMessageDialog(null, "共需支付市场开拓费用" + total + "M");
					this.changeStep();
					this.dashBoardPanel.setDashBoardData();
				}else{
					this.changeStep();
				}
			}

		}
		
		/***ISO认证  ****/
		else if (e.getSource() == btn[30]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 31) {
				JCheckBox iso9000CheckBox = new JCheckBox("ISO9000");
				JCheckBox iso14000CheckBox = new JCheckBox("ISO14000");
				int team_Id = Erps.getCurrentTeam().getId();
				ResearchService researchService = Erps.getIocInstance().get(ResearchServiceImpl.class);
				if (researchService.findResearchs(team_Id, "ISO9000") == 1) {
					iso9000CheckBox.setEnabled(false);
				}
				if (researchService.findResearchs(team_Id, "ISO14000") == 2) {
					iso14000CheckBox.setEnabled(false);
				}
				JPanel panel = new JPanel();
				panel.add(iso9000CheckBox);
				panel.add(iso14000CheckBox);
				JPanel researchPanel = new JPanel(new BorderLayout());
				researchPanel.add(new JLabel("请选择需要开拓的市场?"), BorderLayout.CENTER);
				researchPanel.add(panel, BorderLayout.SOUTH);
				int n = JOptionPane.showConfirmDialog(null, researchPanel, "ISO认证投资", JOptionPane.OK_CANCEL_OPTION);
				if (n == JOptionPane.YES_OPTION) {
					int total = 0;
					if (iso9000CheckBox.isSelected()) {
						Research research = new Research();
						research.setTeam_Id(Erps.getCurrentTeam().getId());
						research.setType("ISO9000");
						research.setTime(Erps.getSand().getTime());
						research.setAmount(1);
						research.setName("ISO认证");
						researchService.researchInvest(research);
						total = total + 1;
					}
					if (iso14000CheckBox.isSelected()) {
						Research research = new Research();
						research.setTeam_Id(Erps.getCurrentTeam().getId());
						research.setType("ISO14000");
						research.setTime(Erps.getSand().getTime());
						research.setAmount(1);
						research.setName("ISO认证");
						researchService.researchInvest(research);
						total = total + 1;
					}
	
					JOptionPane.showMessageDialog(null, "共需支付ISO认证费用" + total + "M");
					this.changeStep();
					this.dashBoardPanel.setDashBoardData();
				}else{
					this.changeStep();
				}
			}
		}
		/*** 关账 ****/
		else if (e.getSource() == btn[31]) {
			if (Integer.parseInt(Erps.getSand().getStep()) == 32) {				
				Sand sand = Erps.getSand();					
				sand.setStep("1");
				sand.setTime(CalculateQuarter.addOneQuarter(Erps.getSand().getTime()));
				sandService.updateSand(sand);
				this.btn[31].setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
				this.btn[0].setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.green));	
				this.dashBoardPanel.setDashBoardData();
			}
		}
	}
}
